#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@File: FlowManger.py
@Author: lijk34925 
@Date: 2025/3/25 16:58
@desc: 流程管理模块功能说明
"""
import time

from AmlScripts.BasicFunction.common_test_prepare import common_test_prepare
from AmlScripts.WorkFlow.WorkFlowDeal import WorkFlowDeal
from BladeFerm.Lib.Common import data_processing
from qturboframe.lib.webdriver import SeleniumError
from BladeFerm.hui12_common.function import display_wait, scroll_to_element_in_contains
from BladeFerm.hui2_app import FE


@data_processing
@common_test_prepare
def flow_deal_operation(input_data=None, ui_expect=None):
    """
    客户风险等级评审流程处理: 填写流程意见和操作
    :param input_data:
    :param ui_expect:
    :return:
    """
    operator = input_data.pop('操作', '初评')
    return_node_name = input_data.pop('退回节点', '初审岗')
    # 步骤1：填写评审意见
    container_xpath = '//div[@class="wf-task-tab"]'
    panel_div = '//label[contains(text(),"审批意见")]'
    display_wait(idname={'xpath': panel_div}, flag=0, timeout=10, driver=FE.driver)
    # 滚动到页面的特定位置，例如滚动到底部
    # scroll_element_into_container_view(FE.driver, panel_div, container_xpath)
    scroll_to_element_in_contains(FE.driver, container_xpath, panel_div)
    # 填写流程意见
    info_view = input_data.get("审批意见", "同意")
    text_element = FE.textarea(idname={'xpath': "//label[contains(text(),'审批意见')]/..//div[@class='h-input-wrapper']/textarea"})
    text_element.set_value(info_view)
    # 步骤2：点击操作按钮
    btn_element = (f'//div[@class="foot-opt" and (not(@style) or not(contains(@style, "display: none")))]'
                   f'//button/span[contains(text(),"{operator}")]')
    FE.button(idname={'xpath': btn_element}).click()
    # 步骤3：点击确定按钮
    if operator in ["初评", "复评", "提交审定", "审定"]:
        FE.msg_box("是否提交流程").click('确定')
        FE.msg_box("成功").click('确定')
    elif operator == "保存":
        FE.msg_box("成功").click('确定')
    elif operator == "退回":
        # 退回流程：先选择退回节点，再点击确认
        table_top_xpath = '//div[@class="h-modal-header"]/div[contains(text(),"退回设置")]/../..'
        layer_element = FE.list(idname={'xpath': table_top_xpath}, father=FE.driver)
        FE.button(idname={'xpath': f'//td/div/span[contains(text(),"{return_node_name}")]'},
                  father=layer_element.element).click()
        time.sleep(0.3)
        FE.button(text='提交', father=layer_element.element).click()
        time.sleep(0.3)
        FE.msg_box("是否退回流程").click('确定')
        time.sleep(0.1)
        FE.msg_box("是否退回").click('确定')
        time.sleep(0.1)
        FE.msg_box("成功").click('确定')
    else:
        raise SeleniumError(f'操作类型{operator}不正确!')


def flow_deal_common(input_data=None, ui_expect=None):

    WF = WorkFlowDeal()